package com.hrt.datasender.datastorage.strategy.monitordata;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.btcode.common.PropertiesUnit;
import com.btcode.db.result.Row;
import com.btcode.orm.model.IModel;
import com.hrt.datasender.datadao.model.BdS60RtModel;
import com.hrt.datasender.datadao.model.BdSourceModel;
import com.hrt.datasender.datadao.service.BdSendRecordService;
import com.hrt.datasender.datadao.service.BdSourceSingleService;
import com.hrt.datasender.datastorage.StorageDataType;
import com.hrt.datasender.web.calSetting.CalSettingService;

public class S60RTNOX extends MonitorDataStrategy {
    private final int dataInterval = 60;

    @Override
    public void registerClearShedule() {
        S60RTClear s60RTc = new S60RTClear();
        s60RTc.registerJob();
    }

    @Override
    public List<Row> getSendData(com.hrt.datasender.datadao.model.BcSendPlatformModel sendPlatForm) {
        Long readySendDatatime = getReadySendDataTime(sendPlatForm);

        if (readySendDatatime == null) {
            return null;
        }

        List<Row> result = getData(sendPlatForm, readySendDatatime);

        return result;
    };

    @Override
    public List<IModel> getStorageData(Calendar cal,List<String> parmList) {

        String iscoefficient = PropertiesUnit.getValue("conf", "iscoefficient") + "";
        iscoefficient = iscoefficient.toLowerCase();

        List<BdSourceModel> sourceData = getSourceData();

        if (sourceData == null) {
            log.info("暂无原始数据");
            return null;
        }

        List<BdS60RtModel> resultData = null;

        /**
         * 如果需要计算折算值
         */
        if (iscoefficient.equals("true")) {
            resultData = processZS(sourceData);
        }

        /**
         * 如果不需要折算值，或者经过折算值处理后仍为空
         */
        if (resultData == null || iscoefficient.equals("false")) {
            resultData = processNormal(sourceData);
        }

        if (resultData == null || resultData.size() == 0) {
            return null;
        }

        List<IModel> finalResult = new ArrayList<>();

        for (BdS60RtModel model : resultData) {
            model.setFlag(CalSettingService.getInstance().isCalData(cal.getTime(),model.getParamCodeBus()));
            finalResult.add(model);
        }

        return finalResult;
    }

    /**
     * 从原始数据表中获取数据
     * 
     * @return
     */
    public List<BdSourceModel> getSourceData() {

        List<BdSourceModel> sourceData = BdSourceSingleService.getInstance().getLastSourceData();

        return sourceData;
    }

    /**
     * 进行折算值处理
     * 
     * @return
     */
    public List<BdS60RtModel> processZS(List<BdSourceModel> sourceDatas) {

        if (sourceDatas == null) {
            return null;
        }

        /**
         * 系数值
         */
        String coefficient = PropertiesUnit.getValue("conf", "coefficient");
        Double coefficientDouble = Double.valueOf(coefficient);
        /**
         * 含氧代码
         */
        String oxygencontent = PropertiesUnit.getValue("conf", "oxygencontent");
        Double oxy = 0.0;
        /**
         * 取含氧系数值
         */
        for (int j = 0; j < sourceDatas.size(); j++) {
            BdSourceModel item = sourceDatas.get(j);
            if (oxygencontent.equals(item.getParamCodeBus())) {
                oxy = item.getMeasRe();
                break;
            }
        }

        if (!isEmpty(oxy)) {
            return null;
        }

        if (oxy == 0.0) {
            return null;
        }

        List<BdS60RtModel> result = new ArrayList<>();

        // 确保一组数据datatime一致
        Date currentTime = new Date();

        for (int i = 0; i < sourceDatas.size(); i++) {
            BdSourceModel sourceData = sourceDatas.get(i);

            Double measReal = sourceData.getMeasRe();
            Double measZS = sourceData.getMeasZs();

            BdS60RtModel data = new BdS60RtModel();
            data.setDataId(sourceData.getDataId());
            data.setParamCodeBus(sourceData.getParamCodeBus());
            data.setDatatime(currentTime);
            data.setCreatetime(currentTime);
            result.add(data);
            /**
             * 折算与实测值都有
             */
            if (isEmpty(measReal) && isEmpty(measZS)) {
                data.setFlag("N");
                data.setMeasRe(sourceData.getMeasRe());
                data.setMeasAvg(sourceData.getMeasAvg());
                data.setMeasMax(sourceData.getMeasMax());
                data.setMeasMin(sourceData.getMeasMin());
                data.setMeasZs(sourceData.getMeasZs());
                data.setMeasZsAvg(sourceData.getMeasZs());
                data.setMeasZsMax(sourceData.getMeasZs());
                data.setMeasZsMin(sourceData.getMeasZs());
            }
            /**
             * 折算与实测值都没有
             */
            else if (!isEmpty(measReal) && !isEmpty(measZS)) {
                data.setFlag("D");
                data.setMeasRe(sourceData.getMeasRe());
                data.setMeasAvg(sourceData.getMeasAvg());
                data.setMeasMax(sourceData.getMeasMax());
                data.setMeasMin(sourceData.getMeasMin());
                data.setMeasZs(sourceData.getMeasZs());
                data.setMeasZsAvg(sourceData.getMeasZs());
                data.setMeasZsMax(sourceData.getMeasZs());
            }
            /**
             * 有实测值 实测值 = （20.9-氧含量）/ 系数 *折算值 折算值 = 实测值 / ((20.9-氧含量)/系数)
             */
            else if (isEmpty(measReal)) {
                data.setMeasRe(sourceData.getMeasRe());
                data.setMeasAvg(sourceData.getMeasAvg());
                data.setMeasMax(sourceData.getMeasMax());
                data.setMeasMin(sourceData.getMeasMin());
                Double zsValue = sourceData.getMeasRe() / ((20.9 - oxy) / coefficientDouble);
                data.setMeasZs(zsValue);
                data.setMeasZsAvg(zsValue);
                data.setMeasZsMax(zsValue);
                data.setMeasZsMin(zsValue);
                data.setFlag("N");
            } 
            else if (isEmpty(measZS)) {
                data.setMeasZs(sourceData.getMeasZs());
                data.setMeasZsAvg(sourceData.getMeasZs());
                data.setMeasZsMax(sourceData.getMeasZs());
                data.setMeasZsMin(sourceData.getMeasZs());
                Double reValue = (20.9 - oxy) / coefficientDouble * sourceData.getMeasZs();
                data.setMeasRe(reValue);
                data.setMeasAvg(reValue);
                data.setMeasMax(reValue);
                data.setMeasMin(reValue);
                data.setFlag("N");
            } 
            else {
                return null;
            }
        }

        return result;
    }

    /**
     * 进行普通处理
     * 
     * @return
     */
    public List<BdS60RtModel> processNormal(List<BdSourceModel> sourceDatas) {

        if (sourceDatas == null) {
            return null;
        }

        List<BdS60RtModel> result = new ArrayList<>();

        // 确保一组数据datatime一致
        Date currentTime = new Date();

        Double dNO = 0.0;
        Double dNO2 = 0.0;
        int indexNOX = -1;

        for (int i = 0; i < sourceDatas.size(); i++) {

            BdSourceModel sourceData = sourceDatas.get(i);
            BdS60RtModel data = new BdS60RtModel();
            data.setDataId(sourceData.getDataId());
            data.setParamCodeBus(sourceData.getParamCodeBus());
            data.setMeasRe(sourceData.getMeasRe());
            data.setMeasZs(sourceData.getMeasZs());
            data.setMeasAvg(sourceData.getMeasAvg());
            data.setMeasMax(sourceData.getMeasMax());
            data.setMeasMin(sourceData.getMeasMin());
            data.setDatatime(currentTime);
            data.setFlag("N");
            data.setCreatetime(currentTime);
            result.add(data);

            // NO,NO2数据保存,记录NOX的index
            if ("a21003".equals(data.getParamCodeBus())) {
                dNO = data.getMeasRe();
            } 
            else if ("a21004".equals(data.getParamCodeBus())) {
                dNO2 = data.getMeasRe();
            } 
            else if ("a21002".equals(data.getParamCodeBus())) {
                indexNOX = i;
            }
        }

        if (indexNOX != -1) {
            Double dNOX = dNO + dNO2;
            result.get(indexNOX).setMeasRe(dNOX);
        }

        return result;
    }

    @Override
    public String getStoageTable() {
        return "BD_S60_RT";
    }

    @Override
    public String getNote() {
        return "如果是需要计算折算值站点,需配置conf.properties文件汇总的iscoefficient为true,coefficient值根据站点配置系数,"
                + "oxygencontent含氧量编码配置成该站点一样";
    }

    @Override
    public String getShowName() {
        return "NOX60秒实时数据";
    }

    @Override
    public StorageDataType getDataType() {
        return StorageDataType.SECOND;
    }

    @Override
    public int getDataInterval() {
        return dataInterval;
    }

    @Override
    public String getConsStr() {
        return "0 0/1 * * * ?";
    }

}
